package com.kakarote.hrm.controller;

import cn.hutool.core.collection.ListUtil;
import com.alibaba.fastjson.JSONObject;
import com.kakarote.common.log.annotation.OperateLog;
import com.kakarote.common.log.entity.OperationLog;
import com.kakarote.common.log.entity.OperationResult;
import com.kakarote.common.log.enums.ApplyEnum;
import com.kakarote.common.log.enums.BehaviorEnum;
import com.kakarote.common.log.enums.OperateObjectEnum;
import com.kakarote.common.log.enums.OperateTypeEnum;
import com.kakarote.core.common.Result;
import com.kakarote.core.entity.BasePage;
import com.kakarote.core.entity.UploadEntity;
import com.kakarote.hrm.entity.BO.QueryRecruitPlanRecordPageListBO;
import com.kakarote.hrm.entity.PO.HrmRecruitPlanRecord;
import com.kakarote.hrm.entity.VO.RecruitPlanRecordVO;
import com.kakarote.hrm.service.IHrmRecruitPlanRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * <p>
 * 计划招聘记录 前端控制器
 * </p>
 *
 * @author system
 * @since 2024-01-01
 */
@RestController
@RequestMapping("/hrmRecruitPlanRecord")
@Api(tags = "招聘管理-计划招聘记录")
public class HrmRecruitPlanRecordController {

    @Autowired
    private IHrmRecruitPlanRecordService recruitPlanRecordService;

    @ApiOperation("添加计划招聘记录")
    @PostMapping("/addRecruitPlanRecord")
    @OperateLog(apply = ApplyEnum.HRM, object = OperateObjectEnum.HRM_RECRUIT_PLAN_RECORD, behavior = BehaviorEnum.SAVE)
    public Result addRecruitPlanRecord(@Validated @RequestBody HrmRecruitPlanRecord recruitPlanRecord) {
        OperationLog operationLog = recruitPlanRecordService.addRecruitPlanRecord(recruitPlanRecord);
        return OperationResult.ok(operationLog);
    }

    @ApiOperation("修改计划招聘记录")
    @PostMapping("/setRecruitPlanRecord")
    @OperateLog(apply = ApplyEnum.HRM, object = OperateObjectEnum.HRM_RECRUIT_PLAN_RECORD, behavior = BehaviorEnum.UPDATE)
    public Result setRecruitPlanRecord(@Validated @RequestBody HrmRecruitPlanRecord recruitPlanRecord) {
        OperationLog operationLog = recruitPlanRecordService.setRecruitPlanRecord(recruitPlanRecord);
        return OperationResult.ok(operationLog);
    }

    /**
     * 获取计划招聘记录详情
     *
     * @param recordId 记录id
     */
    @ApiOperation("获取计划招聘记录详情")
    @PostMapping("/queryById/{recordId}")
    public Result<RecruitPlanRecordVO> queryById(@PathVariable("recordId") Long recordId) {
        RecruitPlanRecordVO recruitPlanRecordVO = recruitPlanRecordService.queryById(recordId);
        return Result.ok(recruitPlanRecordVO);
    }

    @ApiOperation("查询计划招聘记录列表")
    @PostMapping("/queryRecruitPlanRecordPageList")
    public Result<BasePage<RecruitPlanRecordVO>> queryRecruitPlanRecordPageList(@RequestBody QueryRecruitPlanRecordPageListBO queryRecruitPlanRecordPageListBO) {
        BasePage<RecruitPlanRecordVO> page = recruitPlanRecordService.queryRecruitPlanRecordPageList(queryRecruitPlanRecordPageListBO);
        return Result.ok(page);
    }

    @ApiOperation("根据计划id查询所有记录列表")
    @PostMapping("/queryByPlanId/{planId}")
    public Result<List<RecruitPlanRecordVO>> queryByPlanId(@PathVariable("planId") Long planId) {
        List<RecruitPlanRecordVO> recordList = recruitPlanRecordService.queryByPlanId(planId);
        return Result.ok(recordList);
    }

    @ApiOperation("删除计划招聘记录")
    @PostMapping("/deleteRecruitPlanRecord/{recordId}")
    @OperateLog(apply = ApplyEnum.HRM, object = OperateObjectEnum.HRM_RECRUIT_PLAN_RECORD, behavior = BehaviorEnum.DELETE)
    public Result deleteRecruitPlanRecord(@PathVariable("recordId") Long recordId) {
        OperationLog operationLog = recruitPlanRecordService.deleteRecruitPlanRecord(recordId);
        return OperationResult.ok(operationLog);
    }

    @ApiOperation("批量删除计划招聘记录")
    @PostMapping("/batchDeleteRecruitPlanRecord")
    @OperateLog(apply = ApplyEnum.HRM, object = OperateObjectEnum.HRM_RECRUIT_PLAN_RECORD, behavior = BehaviorEnum.DELETE)
    public Result batchDeleteRecruitPlanRecord(@RequestBody List<Long> recordIds) {
        OperationLog operationLog = recruitPlanRecordService.batchDeleteRecruitPlanRecord(recordIds);
        return OperationResult.ok(operationLog);
    }

    @ApiOperation("根据计划id删除所有记录")
    @PostMapping("/deleteByPlanId/{planId}")
    @OperateLog(apply = ApplyEnum.HRM, object = OperateObjectEnum.HRM_RECRUIT_PLAN_RECORD, behavior = BehaviorEnum.DELETE)
    public Result deleteByPlanId(@PathVariable("planId") Long planId) {
        OperationLog operationLog = recruitPlanRecordService.deleteByPlanId(planId);
        return OperationResult.ok(operationLog);
    }

    @PostMapping("/exportRecruitPlanRecord")
    @ApiOperation("导出计划招聘记录")
    @OperateLog(apply = ApplyEnum.HRM, object = OperateObjectEnum.HRM_RECRUIT_PLAN_RECORD, behavior = BehaviorEnum.EXCEL_EXPORT, type = OperateTypeEnum.EXPORT)
    public void exportRecruitPlanRecord(@RequestBody QueryRecruitPlanRecordPageListBO queryRecruitPlanRecordPageListBO, HttpServletResponse response) throws IOException {
        recruitPlanRecordService.exportRecruitPlanRecord(response, queryRecruitPlanRecordPageListBO);
    }

    @PostMapping("/downloadExcel")
    @ApiOperation("获取导入模板")
    public void downloadExcel(HttpServletResponse response) throws IOException {
        recruitPlanRecordService.downloadExcel(response);
    }

    @PostMapping("/excelImport")
    @ApiOperation("excel导入招聘记录")
    @OperateLog(apply = ApplyEnum.HRM, behavior = BehaviorEnum.EXCEL_IMPORT, type = OperateTypeEnum.IMPORT, object = OperateObjectEnum.HRM_RECRUIT_PLAN_RECORD)
    public Result<JSONObject> excelImport(@RequestParam("file") MultipartFile file) {
        JSONObject object = recruitPlanRecordService.excelImport(file);

        UploadEntity uploadEntity = recruitPlanRecordService.uploadTempFile(file, null);

        OperationLog operationLog = new OperationLog();
        operationLog.setOperationObject("uploadData", uploadEntity.getFileId());
        operationLog.setOperationObject("name", "招聘记录");

        JSONObject info = new JSONObject();
        info.put("count", object.getInteger("totalSize"));
        info.put("errorSize", object.getInteger("errSize"));
        info.put("errorFile", object.getString("token"));
        operationLog.setOperationInfo(info.toJSONString());
        return OperationResult.ok(object, ListUtil.toList(operationLog));
    }
}

